﻿
// HCViewerProgrammView.cpp: CHCViewerProgrammView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "HCViewerProgramm.h"
#endif
#include <iostream>
#include <fstream>
#include "GlobalInc.h"
#include "HCViewerProgrammDoc.h"
#include "HCViewerProgrammView.h"
#include <time.h>
#include <thread>
#include <sstream>
#include "CNCEditWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
extern ofstream out;
IMPLEMENT_DYNCREATE(CHCViewerProgrammView, CView)

BEGIN_MESSAGE_MAP(CHCViewerProgrammView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()

// CHCViewerProgrammView 构造/析构

CHCViewerProgrammView::CHCViewerProgrammView() noexcept
{
	// TODO: 在此处添加构造代码
	//AfxMessageBox(_T("create view"));
	m_NCEditWnd = NULL;
	m_ViewCreateFlag = 0;
	m_filePath = "";
}

CHCViewerProgrammView::~CHCViewerProgrammView()
{
	//AfxMessageBox(_T("destroy view"));
	m_ViewCreateFlag = 0;
	m_filePath = "";
	if (m_NCEditWnd)
	{
		delete m_NCEditWnd;
		m_NCEditWnd = NULL;
	}
}

BOOL CHCViewerProgrammView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}
std::string doubleToString(const double& val)
{
	char* chCode;
	chCode = new char[100];
	sprintf_s(chCode, 100, "%.3lf", val);
	std::string str(chCode);
	delete[]chCode;
	return str;
}
// CHCViewerProgrammView 绘图
// CMFTestView 消息处理程序
int CHCViewerProgrammView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	//AfxMessageBox(_T("test 1"));
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	return 0;
}
vector<CGcode> CHCViewerProgrammView::getVgcode()
{
	return m_Vgcode;
}
string CHCViewerProgrammView::getFilePath()
{
	return m_filePath;
}
void CHCViewerProgrammView::OnDraw(CDC* pDC)
{
	clock_t start;
	clock_t end;
	double execution_time = 0;
	CHCViewerProgrammDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;
	// TODO: 在此处为本机数据添加绘制代码
#if 1
	if (m_ViewCreateFlag == 0) {
		m_ViewCreateFlag = 1;
		RECT rect;
		GetClientRect(&rect);
		m_NCEditWnd = new CNCEditWnd();
		m_NCEditWnd->Create(WS_EX_CLIENTEDGE, rect, this);
		m_NCEditWnd->InitNcEdit(12, _T("Fixedsys"));
		m_NCEditWnd->ShowWindow(SW_SHOW);
		//std::vector<CGcode> m_vCGcode;
		m_filePath = CT2A(pDoc->GetPathName().GetString());
		//if (cParseGcodeByFile)
		{
			start = clock();
			int ret = parseGcodeByFile(m_filePath, m_Vgcode);
			end = clock();
			execution_time = (double)(end - start);
			out << "time used by parse file" << execution_time << endl;
		}
		//drawGCodeMultiLine();
		out << "m_vCGcode size:" << m_Vgcode.size() << endl;
		out << "-------------------------------" << endl;

		for (auto it : m_Vgcode)
		{
			out << "m_text " << it.getMtext() << "m_cmdtype " << it.getCmdType() << endl;
		}
		start = clock();
		m_NCEditWnd->OpenFile(pDoc->GetPathName());
	//	std::thread a(drawThreadGCodeMultiLine,this);
	//	a.join();
		end = clock();
		execution_time = (double)(end - start);
		out << "time used by show parse" << execution_time << endl;
		out << "-------------------------------" << endl;
	}
	
#endif
	// TODO: 在此处为本机数据添加绘制代码
}


// CHCViewerProgrammView 打印

BOOL CHCViewerProgrammView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CHCViewerProgrammView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CHCViewerProgrammView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CHCViewerProgrammView 诊断

#ifdef _DEBUG
void CHCViewerProgrammView::AssertValid() const
{
	CView::AssertValid();
}

void CHCViewerProgrammView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CHCViewerProgrammDoc* CHCViewerProgrammView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHCViewerProgrammDoc)));
	return (CHCViewerProgrammDoc*)m_pDocument;
}
#endif //_DEBUG


// CHCViewerProgrammView 消息处理程序

void CHCViewerProgrammView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);
	if (m_NCEditWnd)
	{
		RECT rect;
		GetClientRect(&rect);
		m_NCEditWnd->MoveWindow(&rect);
	}
	// TODO: 在此处添加消息处理程序代码
}
